package org.example.petsystem.controller;
import org.example.petsystem.entity.ResultBody;
import org.example.petsystem.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/Collection")
@CrossOrigin(origins="*")
public class CollectionController {
    @Autowired
    private CollectionService collectionService;
    //加入购物车
    @RequestMapping("/collectionGoods")
    public ResultBody collectionGoods(@RequestBody Map<String,Object> map) {
        if(map.get("code")==null||map.get("phone")==null)
        {
            return new ResultBody (404,"参数错误",false);
        }
        //商品编号，买家电话号码
        String code = map.get("code").toString();
        String phone = map.get("phone").toString();
        if(collectionService.addCollection(code,phone)>0)
        {
            return new ResultBody(200,"加入购物车成功",true);
        }
        else
            return new ResultBody(400,"加入购物车失败",false);
    }
    //移除购物车
    @RequestMapping("/Delcollection")
    public ResultBody Delcollection(@RequestBody Map<String,Object> map) {
        if(map.get("code")==null)
        {
            return new ResultBody(404,"参数错误",false);
        }
        //商品编号
        String code = map.get("code").toString();
        if(collectionService.DelCollection(code)>0)
        {
            return new ResultBody(200,"移除购物车成功",true);
        }
        else
            return new ResultBody(400,"移除购物车失败",false);
    }
    //展示收藏商品
    @RequestMapping("/displayCollection")
    public ResultBody displayCollection(@RequestBody Map<String,Object> map) {
        if(map.get("phone")==null)
        {
            return new ResultBody(404,"参数错误",false);
        }
        //买家电话号码
        String phone = map.get("phone").toString();
        return collectionService.displayCollection(phone);
    }
}
